<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import service from '@/utils/request'
import VideoMonitor from '@/components/dashboard/VideoMonitor.vue'
import EnvironmentChart from '@/components/dashboard/EnvironmentChart.vue'

import AlertList from '@/components/dashboard/AlertList.vue'
import GreenhouseInfo from '@/components/dashboard/GreenhouseInfo.vue'
import DeviceMonitor from '@/components/dashboard/DeviceMonitor.vue'
import VideoHistory from '@/components/dashboard/VideoHistory.vue'
import ParticleBackground from '@/components/auth/ParticleBackground.vue'

const router = useRouter()
const currentTime = ref(new Date().toLocaleString())
const temperature = ref('15°C')

// 更新时间
setInterval(() => {
  currentTime.value = new Date().toLocaleString()
}, 1000)

const videoHistoryRef = ref(null)

// 处理抓拍成功事件
const handleCaptureSuccess = () => {
  // 调用VideoHistory组件的刷新方法
  videoHistoryRef.value?.fetchHistoryImages()
}

// 添加退出登录方法
const handleLogout = async () => {
  try {
    await service.post('/api/auth/logout')
    // 清除本地存储的 token
    localStorage.removeItem('token')
    // 清除其他可能的认证信息
    sessionStorage.clear()
    // 强制刷新路由
    window.location.href = '/login'
  } catch (error) {
    console.error('退出登录失败:', error)
  }
}
</script>

<template>
  <div class="big-screen">
    <particle-background />
    
    <div class="header">
      <div class="logo-title">
        <h1>智慧农业大棚监控平台</h1>
        <div class="subtitle">Smart Agriculture Monitoring System</div>
      </div>
      <div class="header-right">
        <div class="time-box">
          <i class="el-icon-time" />
          <div class="time">{{ currentTime }}</div>
        </div>
        <el-button
          type="danger"
          size="small"
          class="logout-button"
          @click="handleLogout"
        >
          <i class="el-icon-switch-button" />
          退出登录
        </el-button>
      </div>
    </div>

    <div class="content">
      <!-- 左侧面板 -->
      <div class="left-section">
        <greenhouse-info />
        <device-monitor />
      </div>

      <!-- 中间面板 - 视频区域 -->
      <div class="center-section">
        <video-monitor @capture-success="handleCaptureSuccess" />
        <video-history ref="videoHistoryRef" />
      </div>

      <!-- 右侧面板 -->
      <div class="right-section">
        <environment-chart />
        <alert-list />
      </div>
    </div>
  </div>
</template>

<style scoped>
.big-screen {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: transparent;
  color: #fff;
  display: flex;
  flex-direction: column;
  overflow: auto;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 40px;
  background: rgba(13, 27, 42, 0.6);
  border-bottom: 1px solid rgba(48, 243, 249, 0.2);
  backdrop-filter: blur(8px);
  flex-shrink: 0;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
}

.logo-title {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

h1 {
  font-size: 28px;
  color: #30f3f9;
  margin: 0;
  white-space: nowrap;
  text-shadow: 0 0 10px rgba(48, 243, 249, 0.5);
  letter-spacing: 2px;
}

.subtitle {
  font-size: 14px;
  color: rgba(48, 243, 249, 0.7);
  letter-spacing: 1px;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 30px;
}

.time-box {
  display: flex;
  align-items: center;
  gap: 10px;
  background: rgba(48, 243, 249, 0.1);
  padding: 8px 15px;
  border-radius: 20px;
  border: 1px solid rgba(48, 243, 249, 0.2);
}

.time {
  font-size: 16px;
  color: #30f3f9;
  white-space: nowrap;
  text-shadow: 0 0 10px rgba(48, 243, 249, 0.3);
}

.content {
  display: grid;
  grid-template-columns: 1fr 2fr 1.2fr;
  gap: 20px;
  padding: 20px;
  min-height: calc(100vh - 80px);
  position: relative;
  z-index: 1;
}

.left-section,
.center-section,
.right-section {
  display: flex;
  flex-direction: column;
  gap: 20px;
  min-height: 0;
  overflow: auto;
}

/* 面板样式优化 */
:deep(.panel) {
  background: rgba(13, 27, 42, 0.6);
  border: 1px solid rgba(48, 243, 249, 0.2);
  border-radius: 12px;
  backdrop-filter: blur(8px);
  box-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.37);
  transition: all 0.3s ease;
}

:deep(.panel:hover) {
  border-color: rgba(48, 243, 249, 0.4);
  box-shadow: 0 8px 32px 0 rgba(48, 243, 249, 0.2);
}

:deep(.panel-header) {
  border-bottom: 1px solid rgba(48, 243, 249, 0.2);
  padding: 15px 20px;
  font-size: 16px;
  color: #30f3f9;
  text-shadow: 0 0 10px rgba(48, 243, 249, 0.3);
}

.logout-button {
  display: flex;
  align-items: center;
  gap: 5px;
  background: rgba(245, 108, 108, 0.2);
  border: 1px solid rgba(245, 108, 108, 0.4);
  transition: all 0.3s ease;
}

.logout-button:hover {
  background: rgba(245, 108, 108, 0.3);
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(245, 108, 108, 0.2);
}

/* 滚动条样式优化 */
:deep(::-webkit-scrollbar) {
  width: 6px;
  height: 6px;
}

:deep(::-webkit-scrollbar-track) {
  background: rgba(13, 27, 42, 0.1);
  border-radius: 3px;
}

:deep(::-webkit-scrollbar-thumb) {
  background: rgba(48, 243, 249, 0.3);
  border-radius: 3px;
}

:deep(::-webkit-scrollbar-thumb:hover) {
  background: rgba(48, 243, 249, 0.5);
}

/* 保持粒子背景固定 */
:deep(.particle-background) {
  position: fixed;
  z-index: 0;
}
</style> 